package com.antgroup.seckill.business.goods.service.impl;

import com.antgroup.seckill.business.goods.mapper.SeckillOrderMapper;
import com.antgroup.seckill.business.goods.model.SeckillGoods;
import com.antgroup.seckill.business.goods.model.SeckillOrder;
import com.antgroup.seckill.business.goods.model.User;
import com.antgroup.seckill.business.goods.service.ISeckillOrderService;
import com.antgroup.seckill.business.utils.MD5Util;
import com.antgroup.seckill.business.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangxy
 * @since 2024-07-30
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Override
    public SeckillOrder querySeckillOrderByUserId(String id) {
        QueryWrapper<SeckillOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);

        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(queryWrapper);
        return seckillOrder;
    }


    /**
     * 查询用户秒杀商品是否成功
     * orderId:成功，-1：失败，0：下单中
     * @param user
     * @param goodsId
     * @return
     */
    @Override
    public String querySeckillGoodsOrder(User user, String goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrder>().eq("goods_id", goodsId)
                .eq("user_id", user.getId()));
        if(Objects.nonNull(seckillOrder)) {
            return seckillOrder.getOrderId();
        }else if(redisTemplate.hasKey("isStockEmpty:" + goodsId)) {
            return String.valueOf(-1);
        }
        return String.valueOf(0);
    }

    /**
     * 生成秒杀实际地址
     *
     * @param user
     * @param goodsId
     * @return
     */
    @Override
    public String createSeckillPath(User user, String goodsId) {
        String md5 = MD5Util.getMd5(UUIDUtil.uuid() + "1a2b3c4d");
        redisTemplate.opsForValue().set("seckillPath:" + user.getId() + ":" + goodsId,md5,60, TimeUnit.SECONDS);
        return md5;
    }

    /**
     *  校验秒杀地址是否正确
     * @param user
     * @param goodsId
     * @param path
     * @return
     */
    @Override
    public boolean checkSeckillPath(User user, String goodsId, String path) {
        String redisPath = (String) redisTemplate.opsForValue().get("seckillPath:" + user.getId() + ":" + goodsId);
        return StringUtils.equals(path,redisPath);
    }

    /**
     * 校验验证码
     *
     * @param user
     * @param goodsId
     * @param captcha
     * @return
     */
    @Override
    public boolean checkCaptch(User user, String goodsId, String captcha) {
        if(StringUtils.isEmpty(captcha)) {
            return false;
        }
        String redisCaptch = (String)redisTemplate.opsForValue().get("captcha:" + user.getId() + ":" + goodsId);

        return StringUtils.equals(redisCaptch,captcha);
    }
}
